﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Trade.DAL;
using Trade.Model;

namespace Trade
{
    public partial class FrmEmployee : Trade.Controls.RayForm
    {
        private Employee employee = new Employee();
        //private EmployeeInfo employeeInfo;
        private Utility.CRUD dataState = Utility.CRUD.None; //测试
        //private string dataState = Utility.CRUDAction(Utility.CRUD.None);
        private Common common = new Common();
        private DataTable dtEmployee = null;

        public FrmEmployee()
        {
            InitializeComponent();
            common.SetButtonOperatorGroup(dataState, btnOperatorGroup);
            common.SetControlsState(this, false);

            btnOperatorGroup.btnNew.Click += new EventHandler(btnNew_Click);
            btnOperatorGroup.btnModify.Click += new EventHandler(btnModify_Click);
            btnOperatorGroup.btnDelete.Click += new EventHandler(btnDelete_Click);
            btnOperatorGroup.btnSave.Click += new EventHandler(btnSave_Click);
            btnOperatorGroup.btnCancel.Click += new EventHandler(btnCancel_Click);
            btnOperatorGroup.btnQuit.Click += new EventHandler(btnQuit_Click);
        }

        //退出
        private void btnQuit_Click(object sender, EventArgs e)
        {
            if (dataState != Utility.CRUD.None)
            {
                if (MessageBox.Show("当前数据还未保存,是否需要保存数据?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.Yes)
                {
                    btnOperatorGroup.btnSave.Focus();
                    return;
                }
            }

            this.Close();
        }

        //取消
        private void btnCancel_Click(object sender, EventArgs e)
        {
            dataState = Utility.CRUD.None;
            common.SetButtonOperatorGroup(dataState, btnOperatorGroup);
            common.SetControlsState(this, false);
            dgvEmployee.Enabled = true;
            dgvEmployee.Focus();
        }

        //保存
        private void btnSave_Click(object sender, EventArgs e)
        {
            int recordId = 0;

            if (String.IsNullOrEmpty(txtEmployeeNo.Text.Trim()))
            {
                MessageBox.Show("员工编号不能为空！", "Prompt ...");
                txtEmployeeNo.Focus();
                return;
            }

            if (String.IsNullOrEmpty(txtChineseName.Text.Trim()))
            {
                if (String.IsNullOrEmpty(txtChineseName.Text.Trim()))
                {
                    MessageBox.Show("员工姓名不能为空！", "Prompt ...");
                    txtChineseName.Focus();
                    return;
                }
            }

            EmployeeInfo employeeInfo = new EmployeeInfo();
            employeeInfo.EmployeeNo = txtEmployeeNo.Text.Trim();
            employeeInfo.ChineseName = txtChineseName.Text.Trim();
            employeeInfo.EnglishName = txtEnglishName.Text.Trim();
            employeeInfo.Title = cmbTitle.SelectedValue.ToString();
            employeeInfo.Gender = cmbGender.Text.Trim();
            employeeInfo.Tel = txtTel.Text.Trim();
            employeeInfo.Mobile = txtMobile.Text.Trim();
            employeeInfo.Address = txtAddress.Text.Trim();
            employeeInfo.ZipCode = txtZipCode.Text.Trim();
            employeeInfo.IdCardNo = txtIdCardNo.Text.Trim();
            employeeInfo.HireDate = Convert.ToDateTime(txtHireDate.Text.Trim());
            employeeInfo.Department = cmbDepartment.Text.Trim();
            employeeInfo.LoginId = cmbLoginId.Text.Trim();
            employeeInfo.RecordId = dataState == Utility.CRUD.Insert ? 0 : (int)dgvEmployee.CurrentRow.Cells["RecordId"].Value;

            employee.WriteToDatabase(Utility.CRUDAction(dataState), employeeInfo, out recordId);

            if (dataState == Utility.CRUD.Insert)
            {
                DataRow dr = employee.GetEmployeeByRecordId(recordId);
                dtEmployee.ImportRow(dr);
                dgvEmployee.CurrentRow.Selected = false;
                dgvEmployee.Rows[dgvEmployee.RowCount - 1].Selected = true;
                dgvEmployee.CurrentCell = dgvEmployee.Rows[dgvEmployee.RowCount - 1].Cells["EmployeeNo"];     //让当前选中行的Cell得到焦点
            }
            else if (dataState == Utility.CRUD.Update)
            {
                dgvEmployee.CurrentRow.Cells["EmployeeNo"].Value = employeeInfo.EmployeeNo;
                dgvEmployee.CurrentRow.Cells["ChineseName"].Value = employeeInfo.ChineseName;
                dgvEmployee.CurrentRow.Cells["EnglishName"].Value = employeeInfo.EnglishName;
                dgvEmployee.CurrentRow.Cells["Title"].Value = employeeInfo.Title;
                dgvEmployee.CurrentRow.Cells["Gender"].Value = employeeInfo.Gender;
                dgvEmployee.CurrentRow.Cells["Tel"].Value = employeeInfo.Tel;
                dgvEmployee.CurrentRow.Cells["Mobile"].Value = employeeInfo.Mobile;
                dgvEmployee.CurrentRow.Cells["Address"].Value = employeeInfo.Address;
                dgvEmployee.CurrentRow.Cells["ZipCode"].Value = employeeInfo.ZipCode;
                dgvEmployee.CurrentRow.Cells["IdCardNo"].Value = employeeInfo.IdCardNo;
                dgvEmployee.CurrentRow.Cells["HireDate"].Value = employeeInfo.HireDate;
                dgvEmployee.CurrentRow.Cells["Department"].Value = employeeInfo.Department;
                dgvEmployee.CurrentRow.Cells["LoginId"].Value = employeeInfo.Department;
                dgvEmployee.CurrentRow.Cells["RecordId"].Value = employeeInfo.RecordId;
            }
            else if (dataState == Utility.CRUD.Delete)
            {
                dgvEmployee.Rows.Remove(dgvEmployee.Rows[dgvEmployee.CurrentRow.Index]);
            }

            dataState = Utility.CRUD.None;
            common.SetButtonOperatorGroup(dataState, btnOperatorGroup);
            common.SetControlsState(this, false);
            dgvEmployee.Enabled = true;
            dgvEmployee.Focus();
        }

        //删除
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (dgvEmployee.RowCount != 0)
            {
                if (MessageBox.Show("是否真的删除当前数据?", "删除数据", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.Yes)
                {
                    dataState = Utility.CRUD.Delete;
                    common.SetButtonOperatorGroup(dataState, btnOperatorGroup);
                    dgvEmployee.Enabled = false;
                }
            }
        }

        //编辑
        private void btnModify_Click(object sender, EventArgs e)
        {
            dataState = Utility.CRUD.Update;
            common.SetButtonOperatorGroup(dataState, btnOperatorGroup);
            EmployeeInfo employeeInfo = new EmployeeInfo();
            employeeInfo.RecordId = (int)dgvEmployee.CurrentRow.Cells["RecordId"].Value;

            common.SetControlsState(this, true);
            dgvEmployee.Enabled = false;
            txtEmployeeNo.Enabled = false;
            txtChineseName.Focus();
        }

        //新增
        private void btnNew_Click(object sender, EventArgs e)
        {
            dataState = Utility.CRUD.Insert;
            common.SetButtonOperatorGroup(dataState, btnOperatorGroup);
            common.SetControlsState(this, true);
            dgvEmployee.Enabled = false;

            txtEmployeeNo.Text = "";
            txtChineseName.Text = "";
            txtEnglishName.Text = "";
            cmbTitle.SelectedIndex = 0;
            cmbGender.SelectedIndex = 0;
            txtTel.Text = "";
            txtMobile.Text = "";
            txtAddress.Text = "";
            txtZipCode.Text = "";
            txtIdCardNo.Text = "";
            txtHireDate.Text = DateTime.Now.ToString();
            cmbDepartment.SelectedIndex = 0;
            cmbLoginId.SelectedIndex = 0;

            txtEmployeeNo.Focus();
        }

        private void FrmEmployee_Load(object sender, EventArgs e)
        {
            dtEmployee = employee.GetEmployeeList();

            dgvEmployee.DataSource = dtEmployee;
            dgvEmployee.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            SetEmployeeDataGridView();

            cmbTitle.Tag = "0000";
            cmbGender.Items.AddRange(new object[] { "男", "女" });
            cmbDepartment.Tag = "D000";
            cmbLoginId.Tag = "0000";

            common.ComboBoxBindingDataSource(cmbDepartment, "SubjectName", "SubjectName");

            if (dgvEmployee.RowCount != 0)
            {
                dgvEmployee.CurrentRow.Selected = true;
            }
            dgvEmployee.Focus();
        }

        private void SetEmployeeDataGridView()
        {
            dgvEmployee.AllowUserToAddRows = false;
            dgvEmployee.AllowUserToDeleteRows = false;
            dgvEmployee.ReadOnly = true;

            dgvEmployee.Columns[0].Width = 80;
            dgvEmployee.Columns[0].Name = dtEmployee.Columns["EmployeeNo"].ColumnName.ToString();
            dgvEmployee.Columns[0].HeaderText = "员工编号";
            dgvEmployee.Columns[1].Width = 100;
            dgvEmployee.Columns[1].Name = dtEmployee.Columns["ChineseName"].ColumnName.ToString();
            dgvEmployee.Columns[1].HeaderText = "中文名";
            dgvEmployee.Columns[2].Width = 150;
            dgvEmployee.Columns[2].Name = dtEmployee.Columns["EnglishName"].ColumnName.ToString();
            dgvEmployee.Columns[2].HeaderText = "英文名";
            dgvEmployee.Columns[3].Width = 80;
            dgvEmployee.Columns[3].Name = dtEmployee.Columns["Title"].ColumnName.ToString();
            dgvEmployee.Columns[3].HeaderText = "称谓";
            dgvEmployee.Columns[4].Width = 80;
            dgvEmployee.Columns[4].Name = dtEmployee.Columns["Gender"].ColumnName.ToString();
            dgvEmployee.Columns[4].HeaderText = "性别";
            dgvEmployee.Columns[5].Width = 100;
            dgvEmployee.Columns[5].Name = dtEmployee.Columns["Tel"].ColumnName.ToString();
            dgvEmployee.Columns[5].HeaderText = "固定电话";
            dgvEmployee.Columns[6].Width = 100;
            dgvEmployee.Columns[6].Name = dtEmployee.Columns["Mobile"].ColumnName.ToString();
            dgvEmployee.Columns[6].HeaderText = "移动电话";
            dgvEmployee.Columns[7].Width = 200;
            dgvEmployee.Columns[7].Name = dtEmployee.Columns["Address"].ColumnName.ToString();
            dgvEmployee.Columns[7].HeaderText = "通讯地址";
            dgvEmployee.Columns[8].Width = 80;
            dgvEmployee.Columns[8].Name = dtEmployee.Columns["ZipCode"].ColumnName.ToString();
            dgvEmployee.Columns[8].HeaderText = "邮编";
            dgvEmployee.Columns[9].Width = 120;
            dgvEmployee.Columns[9].Name = dtEmployee.Columns["IdCardNo"].ColumnName.ToString();
            dgvEmployee.Columns[9].HeaderText = "身份证号";
            dgvEmployee.Columns[10].Width = 80;
            dgvEmployee.Columns[10].Name = dtEmployee.Columns["HireDate"].ColumnName.ToString();
            dgvEmployee.Columns[10].HeaderText = "雇佣日期";
            dgvEmployee.Columns[11].Width = 100;
            dgvEmployee.Columns[11].Name = dtEmployee.Columns["Department"].ColumnName.ToString();
            dgvEmployee.Columns[11].HeaderText = "所属部门";
            dgvEmployee.Columns[12].Width = 200;
            dgvEmployee.Columns[12].Name = dtEmployee.Columns["LoginId"].ColumnName.ToString();
            dgvEmployee.Columns[12].HeaderText = "登录账号";
            dgvEmployee.Columns[13].Width = 80;
            dgvEmployee.Columns[13].Name = dtEmployee.Columns["RecordId"].ColumnName.ToString();
            dgvEmployee.Columns[13].HeaderText = "记录编号";

            dgvEmployee.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
        }
    }
}
